package homework1;

import java.util.Arrays;

public class Count {
    private String string;

    public Count() {
    }

    public Count(String string) {
        setString(string);
    }

    public String getString() {
        return string;
    }

    public void setString(String string) {
        this.string = string;
    }

    public void print() {
        // 使用ASCII码进行判断计数
        byte[] bytes = string.getBytes();
        System.out.println("字符串内容转换为byte数组是：" + Arrays.toString(bytes));
        int sum_upper = 0;
        int sum_lower = 0;
        int sum_number = 0;
        int sum_other = 0;
        for (int i : bytes) {
            if (i >= 65 && i <= 90) {
                System.out.println((char) i + "：是大写字母");
                sum_upper++;
            } else if (i >= 97 && i <= 122) {
                System.out.println((char) i + "：是小写字母");
                sum_lower++;
            } else if (i >= 48 && i <= 57) {
                System.out.println((char) i + "：是数字");
                sum_number++;
            } else {
                System.out.println((char) i + "：是其它字符");
                sum_other++;
            }
        }
        System.out.println("大写字母个数是：" + sum_upper);
        System.out.println("小写字母个数是：" + sum_lower);
        System.out.println("数字个数是：" + sum_number);
        System.out.println("其它字符个数是：" + sum_other);
    }

    public void show() {
        //使用正则匹配计数
        int sum_upper = 0;
        int sum_lower = 0;
        int sum_number = 0;
        int sum_other = 0;
        String substring;
        for (int i = 0; i < string.length(); i++) {
            substring = string.substring(i, i + 1);
            if (substring.matches("[A-Z]")) {
                System.out.println(substring + "：是大写字母");
                sum_upper++;
            } else if (substring.matches("[a-z]")) {
                System.out.println(substring + "：是小写字母");
                sum_lower++;
            } else if (substring.matches("[0-9]")) {
                System.out.println(substring + "：是数字");
                sum_number++;
            } else {
                System.out.println(substring + "：是其它字符");
                sum_other++;
            }
        }
        System.out.println("大写字母个数是：" + sum_upper);
        System.out.println("小写字母个数是：" + sum_lower);
        System.out.println("数字个数是：" + sum_number);
        System.out.println("其它字符个数是：" + sum_other);
    }

    public void char_method() {
        //使用Character方法计数
        char[] chars = string.toCharArray();
        System.out.println("字符串内容转换为char数组是：" + Arrays.toString(chars));
        int sum_upper = 0;
        int sum_lower = 0;
        int sum_number = 0;
        int sum_other = 0;
        for (char i : chars) {
            if (Character.isUpperCase(i)) {
                System.out.println(i + "：是大写字母");
                sum_upper++;
            } else if (Character.isLowerCase(i)) {
                System.out.println(i + "：是小写字母");
                sum_lower++;
            } else if (Character.isDigit(i)) {
                System.out.println(i + "：是数字");
                sum_number++;
            } else {
                System.out.println(i + "：是其它字符");
                sum_other++;
            }
        }
        System.out.println("大写字母个数是：" + sum_upper);
        System.out.println("小写字母个数是：" + sum_lower);
        System.out.println("数字个数是：" + sum_number);
        System.out.println("其它字符个数是：" + sum_other);
    }
}
